﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Xml;
using FinPlusAssembler;
using FinPlusInterfaces;

namespace FinPlusBaseCore
{
    public static class BeanExtension
    {
        public static string Display(this IBean bean, string defaultNumFormat = "#,##0.########;-#,##0.########;0;0")
        {
            try
            {
                if (bean.Type.Equals(typeof(double)))
                    return double.Parse(bean.ToString()).ToString(defaultNumFormat);
                else if (bean.Type.Equals(typeof(DateTime)))
                    return ((DateTime)bean.Object).FormatDateTime();

                return bean.ToString();
            }
            catch
            {
                return bean.ToString();
            }
        }

        public static void SetDisplay(this IBean bean, string value, bool toOADate = true)
        {
            if (PodField.Type.ToString().Equals(bean.Name) || PodField.Id.ToString().Equals(bean.Name))
                value = bean.ToString();

            if (toOADate && bean.Type.Equals(typeof(DateTime)))
                value = Convert.ToDateTime(value).ToOADate().ToString();

            bean.TimeStamp = DateTime.MinValue;
            bean.Value = value;
        }
    }
}
